package app

import (
	"gina/app/common/request"
	"gina/app/common/response"
	"gina/app/services"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
)

// Register 注册用户
// @Summary 注册用户
// @Description 用户注册接口，接收用户注册信息并创建新用户
// @Tags auth
// @Accept json
// @Produce json
// @Param request body request.Register true "注册信息"
// @Success 200 {object} models.User "注册成功返回用户信息"
// @Failure 400 {string} string "请求参数错误"
// @Failure 500 {string} string "内部服务器错误"
// @Router /auth/register [post]
func Register(c *gin.Context) {
	var form request.Register
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	if err, user := services.UserService.Register(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, user)
	}
}

func Info(c *gin.Context) {
	err, user := services.UserService.GetUserInfo(c.Keys["id"].(string))
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}
	response.Success(c, user)
}

func Logout(c *gin.Context) {
	err := services.JwtService.JoinBlackList(c.Keys["token"].(*jwt.Token))
	if err != nil {
		response.BusinessFail(c, "登出失败")
		return
	}
	response.Success(c, nil)
}
